using System;
using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	[Serializable]
	[AddComponentMenu("MaterialUI/Dialogs/Button Section", 100)]
	public class DialogButtonSection
	{
		private Action m_OnAffirmativeButtonClicked;

		private Action m_OnDismissiveButtonClicked;

		[SerializeField]
		private MaterialButton m_AffirmativeButton;

		[SerializeField]
		private MaterialButton m_DismissiveButton;

		private bool m_ShowDismissiveButton;

		public Action onAffirmativeButtonClicked => m_OnAffirmativeButtonClicked;

		public Action onDismissiveButtonClicked => m_OnDismissiveButtonClicked;

		public MaterialButton affirmativeButton => m_AffirmativeButton;

		public MaterialButton dismissiveButton => m_DismissiveButton;

		public void SetButtons(Action onAffirmativeButtonClick, string affirmativeButtonText, Action onDismissiveButtonClick, string dismissiveButtonText)
		{
			SetAffirmativeButton(onAffirmativeButtonClick, affirmativeButtonText);
			SetDismissiveButton(onDismissiveButtonClick, dismissiveButtonText);
		}

		public void SetAffirmativeButton(Action onButtonClick, string text)
		{
			m_OnAffirmativeButtonClicked = onButtonClick;
			m_AffirmativeButton.text.text = text;
		}

		public void SetDismissiveButton(Action onButtonClick, string text)
		{
			if (!string.IsNullOrEmpty(text) || onButtonClick != null)
			{
				m_ShowDismissiveButton = true;
				m_OnDismissiveButtonClicked = onButtonClick;
				m_DismissiveButton.text.text = text;
			}
		}

		public void SetupButtonLayout(RectTransform dialogRectTransform)
		{
			m_DismissiveButton.gameObject.SetActive(m_ShowDismissiveButton);
			LayoutRebuilder.ForceRebuildLayoutImmediate(dialogRectTransform);
			if (m_AffirmativeButton.minWidth + m_DismissiveButton.minWidth + 24f > DialogManager.rectTransform.rect.width - 48f)
			{
				UnityEngine.Object.DestroyImmediate(m_AffirmativeButton.rectTransform.parent.GetComponent<HorizontalLayoutGroup>());
				VerticalLayoutGroup verticalLayoutGroup = m_AffirmativeButton.rectTransform.parent.gameObject.AddComponent<VerticalLayoutGroup>();
				verticalLayoutGroup.padding = new RectOffset(8, 8, 8, 8);
				verticalLayoutGroup.childAlignment = TextAnchor.UpperRight;
				verticalLayoutGroup.childForceExpandHeight = false;
				verticalLayoutGroup.childForceExpandWidth = false;
			}
		}

		public void OnAffirmativeButtonClicked()
		{
			m_OnAffirmativeButtonClicked.InvokeIfNotNull();
		}

		public void OnDismissiveButtonClicked()
		{
			m_OnDismissiveButtonClicked.InvokeIfNotNull();
		}
	}
}
